package com.project.service.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.common.TreeNode;
import com.project.service.BaseService;
import com.project.dao.sys.DepartmentDao;
import com.project.dao.sys.UserDao;
import com.project.entity.sys.Department;
import com.project.entity.sys.User;
import com.project.util.CurrentSessionUtil;
import com.project.util.JacksonUtil;
import com.project.util.ListUtil;
/**
 * 部门service
 * @author
 */
@Transactional
@Service
public class DepartmentService extends BaseService<Department>{

	@Autowired
	private DepartmentDao departmentDao;
	
	@Autowired
	private UserDao userDao;
	
	/**
	 *  查看所有部门
	 * @return
	 * @throws Exception
	 */
	public String queryList()throws Exception{
		List<Department> list = departmentDao.queryList();
		List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		
		for(Department entity:list){
			TreeNode treeNode = new TreeNode();
			treeNode.setId(String.valueOf(entity.getId()));
			treeNode.setpId(entity.getParentId()==null ? "":String.valueOf(entity.getParentId()));
			treeNode.setName(entity.getName());
			treeNode.setChecked(false);
			treeNode.setOpen(true);
			treeNode.setChkDisabled(false);
			treeNode.setCode(entity.getCode());
			treeNode.setDepartmentCode(entity.getDepartmentCode());
			
			treeNodeList.add(treeNode);
		}
		
		return JacksonUtil.objToJson(treeNodeList);
	}
	
	/**
	 * 重复校验
	 * @param name
	 * @param status
	 * @param updateId
	 * @return
	 */
	public String getStatus(String name,String status,String updateId){
		Department department = null;
		if("1".equals(status)){
			department = departmentDao.findByName(name);
		}else if("2".equals(status)){
			department = departmentDao.findByIdAndName(Long.parseLong(updateId), name);
		}
		
		if(department != null){
			return "0";
		}else{
			return "1";
		}
	}
	
	/**
	 * 保存部门
	 * @param department
	 */
	public void addDept(Department department,String deptId){
		department.setCreateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
		department.setCreateTime(new Date());
		department.setUpdateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
		department.setUpdateTime(new Date());
		department.setParentId(Long.parseLong(deptId));
		
		/*生成部门code*/
		Department parentEntity = departmentDao.getById(Long.parseLong(deptId));
		String parentCodet = parentEntity.getCode();
		
		String newCode = getLastCode(deptId);
		if(StringUtils.isNotEmpty(newCode)){
			department.setCode(newCode);
		}else{
			department.setCode(parentCodet+"01");
		}
		
		departmentDao.save(department);
	}
	
	/**
	 * 修改部门
	 * @param department
	 */
	public void updateDept(Department department){
		department.setUpdateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
		department.setUpdateTime(new Date());
		
		departmentDao.update(department);
		
	}
	
	/**
	 * 验证部门是否可以删除
	 * @param id
	 * @return
	 */
	public String whetherCanDelete(String code){
		/*删除父阶段子节点都需要删除，所以需要判断当前节点以及下面所有节点是否有关联user*/
		List<Department> list = departmentDao.queryByCodeList(code);
		
		
		for(Department dept:list){
			List<User> listUser = userDao.findBydeptCode(dept.getCode());
			if(ListUtil.isNotEmpty(listUser)){
				return "false";
			}
		}
		
		return "true";
	}
	
	/**
	 * 删除
	 * @param department
	 */
	public void delete(Department department){
		String code = department.getCode();
		departmentDao.deleteDept(code);
	}
	
	/**
	 * 根据code查询部门
	 * @return
	 */
	public Department findByCode(String code){
		return departmentDao.findByCode(code);
	}
	
	/**
	 * 获取最后一个同级部门code
	 * @return
	 */
	private String getLastCode(String deptId){
		List<Department> list = departmentDao.findByParentId(Long.parseLong(deptId));
		
		String code = "";
		if(ListUtil.isNotEmpty(list)){
			Department dept = list.get(0);
			String tempCode = dept.getCode();
			String heade = tempCode.substring(0, tempCode.length()-2);
			String end  = tempCode.substring(tempCode.length()-2,tempCode.length());
			
			int num = Integer.parseInt(end)+1;
			
			code = heade+String.format("%0"+2+"d", num);  
		}
		
		return code;
	}
	
}
